package cn.ytr.crm.web.controller;

import cn.ytr.crm.query.CustomerChartQueryObject;
import cn.ytr.crm.service.ICustomerChartService;
import cn.ytr.crm.service.IEmployeeService;
import cn.ytr.crm.service.ISystemDictionaryItemService;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/customerChart")
@Controller
public class CustomerChartController {

    @Autowired
    private ICustomerChartService customerChartService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    /**
     * 显示 客户报表列表页
     */
    @RequiresPermissions(value = {"访问:客户报表页面", "customerChart:list"}, logical = Logical.OR)
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") CustomerChartQueryObject qo, Model model) {
        model.addAttribute("pageInfo", customerChartService.selectChart(qo));
        return "customerChart/customerReport";
    }

    /**
     * 显示 客户报表柱状图页(模态框)
     */
    @RequiresPermissions(value = {"访问:客户报表柱状图页", "customerChart:bar"}, logical = Logical.OR)
    @RequestMapping("/bar")
    public String bar(@ModelAttribute("qo") CustomerChartQueryObject qo, Model model) {

        List<Map<String, Object>> maps = customerChartService.selectChart(qo);
        //准备柱状图数据
        ArrayList<Object> xList = new ArrayList<>();
        ArrayList<Object> yList = new ArrayList<>();
        for (Map<String, Object>map : maps) {
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }

        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList", JSON.toJSONString(yList));
        model.addAttribute("groupType", qo.getGroupTypeName());
        return "customerChart/customerReport_bar";
    }

    /**
     * 显示 客户报表饼状图页(模态框)
     */
    @RequiresPermissions(value = {"访问:客户报表饼状图页", "customerChart:pie"}, logical = Logical.OR)
    @RequestMapping("/pie")
    public String pie(@ModelAttribute("qo") CustomerChartQueryObject qo, Model model) {

        List<Map<String, Object>> maps = customerChartService.selectChart(qo);
        //准备饼状图数据
        ArrayList<Object> xList = new ArrayList<>();
        ArrayList<Object> data = new ArrayList<>();
        for (Map<String, Object> map : maps ) {
            //分组数据
            xList.add(map.get("groupType"));

            //客户数
            HashMap<Object, Object> hashMap = new HashMap<>();
            hashMap.put("name", map.get("groupType"));
            hashMap.put("value", map.get("number"));
            data.add(hashMap);
        }

        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("data", JSON.toJSONString(data));
        //分组名称
        model.addAttribute("groupType", qo.getGroupTypeName());
        return "customerChart/customerReport_pie";
    }

    
}









































